package fylder.chat.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

/**
 * 相片裁剪 Created by fylder on 2015/3/26.
 */
public class ChatPictrueTools {

    private static final String TAG = "Pictrue";

    private static String FILE_PATH = "/Android/data/com.pub.parents/cache";// 文件路径
    private static String PICTURE_NAME = "corp_picture.jpg";// 图片

    public static final String IMAGE_FILE_LOCATION = "file://" + Environment.getExternalStorageDirectory().getPath()
            + FILE_PATH + "/" + PICTURE_NAME;
    public static final String PICTURE_PATH = Environment.getExternalStorageDirectory().getPath() + FILE_PATH;// 图片目录
    public static final String PICTURE_FILE_PATH = PICTURE_PATH + "/" + PICTURE_NAME;// 图片文件path

    public static final int CHOOSE_KIT_PICTURE = 1;
    public static final int CHOOSE_BIG_PICTURE = 2;
    public static final int CHOOSE_CAMERA = 3;

    // aspectX aspectY 是宽高的比例
    private static int outputX = 300;
    private static int outputY = 300;
    // outputX outputY 是裁剪图片宽高
    private static int aspectX = 1;
    private static int aspectY = 1;

    /**
     * 从相册获取
     *
     * @param activity
     * @param imageUri
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void getPicture(Activity activity, Uri imageUri) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            activity.startActivityForResult(intent, CHOOSE_KIT_PICTURE);
        } else {
            intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra("scale", true);
            intent.putExtra("return-data", false);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection", false); // no face detection
            activity.startActivityForResult(intent, CHOOSE_BIG_PICTURE);
        }
    }

    /**
     * 从相机获取
     *
     * @param activity
     * @param imageUri
     */
    public static void getCammer(Activity activity, Uri imageUri) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is capture

        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        activity.startActivityForResult(intent, CHOOSE_CAMERA);
    }

    /**
     * 相册回调
     * <p/>
     * 获取图片的绝对路径,再将其保存
     *
     * @param filePath
     * @param context
     */
    public static void resultPictureKit(Uri uri, String filePath, Context context) {

        String imagePath = PictureHelper.getPath(context, uri); // 获取图片的绝对路径
        copyFile(imagePath, filePath);
    }

    /**
     * 相册回调
     *
     * @param data
     * @param filePath
     */
    public static void resultPicture(Intent data, String filePath, Context context) {

    }

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     */
    private static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }

    }

}
